/**  
 * @Project: goicter Maven Webapp
 * @Package com.goicter.actions
 * @Title: BaseAction.java
 * @Description: ����Action
 *
 * @author: Eric Zheng
 * @date 2015-3-15 ����12:43:56
 * @Copyright: Eric Zheng JIMIOPEN.NET. All rights reserved.
 * @version V1.0   
 */
package com.goicter.actions;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.goicter.commons.constants.ConstantCookies;
import com.goicter.commons.utils.http.CookieUtils;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAct extends ActionSupport implements ServletRequestAware, ServletResponseAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8829524882246784712L;

	protected HttpServletRequest request;
	
	protected HttpServletResponse response;
	
	/**
	 * ��¼��������ջ��ʶ
	 */
	protected final static String LOGIN_UNITE_STACK = "loginUniteStack";
	/**
	 * �޵�¼��������ջ��ʶ
	 */
	protected final static String DEFAULT_STACK_IGNORE = "defaultStackIgnore";

	/**
	 * Ĭ�ϳɹ�����
	 */
	protected static final int SUCCESS_CODE = 1;
	/**
	 * ���״̬1����keyֵ
	 */
	protected static final String STATUS_KEY_FIR = "status_fir";
	/**
	 * ���״̬2����keyֵ
	 */
	protected static final String STATUS_KEY_SEC = "status_sec";
	/**
	 * ������keyֵ
	 */
	protected static final String DATA_KEY = "data";

	/**
	 * ����JSON���
	 */
	protected Map<String, Object> results = new HashMap<String, Object>();

	/**
	 * 
	 * @Description: TODO
	 * @return
	 * @author: Eric Zheng
	 * @date 2015-3-15 ����4:38:42
	 */
	public String getInterfaceVersion() {
		String interface_verson = CookieUtils.getCookie(request,
				ConstantCookies.COOKIE_NAME_INTERFACE);
		if (StringUtils.isBlank(interface_verson)) {
			return "";
		}
		return interface_verson;
	}

	/**
	 * 
	 * @Description: TODO
	 * @return
	 * @author: Eric Zheng
	 * @date 2015-3-15 ����4:38:48
	 */
	public String getMerberIdByCookie() {
		String interface_verson = CookieUtils.getCookie(request, ConstantCookies.COOKIE_NAME_VALUE);
		if (StringUtils.isBlank(interface_verson)) {
			return "";
		}
		return interface_verson;
	}

	/**
	 * ��ӳɹ������Ϣ
	 */
	public void putSuccessResult() {
		results.put(STATUS_KEY_FIR, SUCCESS_CODE);
	}

	/**
	 * ��ӳɹ������Ϣ
	 * 
	 * @param statusCode
	 *            ������
	 */
	public void putSuccessResult(int statusCode) {
		results.put(STATUS_KEY_FIR, statusCode);
	}

	/**
	 * ��ӳɹ������Ϣ
	 * 
	 * @param data
	 *            ������
	 */
	public void putSuccessResult(Object data) {
		results.put(STATUS_KEY_FIR, SUCCESS_CODE);
		results.put(DATA_KEY, data);
	}

	/**
	 * ��ӳɹ������Ϣ
	 * 
	 * @param statusCode
	 *            ������
	 * @param data
	 *            ������
	 */
	public void putSuccessResult(int statusCode, Object data) {
		results.put(STATUS_KEY_FIR, statusCode);
		results.put(DATA_KEY, data);
	}

	/**
	 * ���ʧ�ܽ����Ϣ.
	 */
	public void putFailedResult(int status_fir) {
		results.put(STATUS_KEY_FIR, status_fir);
	}

	/**
	 * ���ʧ�ܽ����Ϣ.
	 */
	public void putFailedResult(int status_fir, int status_sec) {
		results.put(STATUS_KEY_FIR, status_fir);
		results.put(STATUS_KEY_SEC, status_sec);
	}

	/**
	 * ���ʧ�ܽ����Ϣ.
	 */
	public void putFailedResult(int status_fir, int status_sec, Object data) {
		results.put(STATUS_KEY_FIR, status_fir);
		results.put(STATUS_KEY_SEC, status_sec);
		results.put(DATA_KEY, data);
	}

	protected HttpServletRequest getRequest() {
		return this.request;
	}

	protected HttpServletResponse getResponse() {
		return this.response;
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	public Map<String, Object> getResults() {
		return results;
	}

	public void putSuccessResult(String msg, Object data) {
		results.put("status", SUCCESS_CODE);
		results.put("msg", msg);
		results.put("data", data);
	}

	public void putFailedResult(String msg, Object data) {
		results.put(STATUS_KEY_FIR, -1);
		results.put("msg", msg);
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void putFailedResult(String msg) {
		results.put(STATUS_KEY_FIR, -1);
		results.put("msg", msg);
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * ��ȡIP��ַ
	 * 
	 * @param request
	 * @return
	 */
	public static String getRemoteIP(HttpServletRequest request) {
		String ipaddr = request.getHeader("X-nginx-real-ip");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Real-IP");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Forwarded-For");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Remote-Addr");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("Proxy-Client-IP");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("WL-Proxy-Client-IP");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("HTTP_X_REAL_IP");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		if (StringUtils.isBlank(ipaddr)) {
			ipaddr = request.getRemoteAddr();
		}
		return ipaddr;
	}
	
}
